package com.ym.b2b.news.service.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.ym.b2b.commons.exception.BaseException;
import com.ym.b2b.commons.service.BaseService;
import com.ym.b2b.commons.util.HtmlHelper;
import com.ym.b2b.news.dao.EmBNewsDAO;
import com.ym.b2b.news.service.EmBNewsService;
import com.ym.b2b.news.sqlbean.EmBNews;
import com.zc.commons.base.Page;
import com.zc.commons.base.Result;

@Service("emBNewsService")
public class EmBNewsServiceImpl extends BaseService implements EmBNewsService {

    @Autowired
    private EmBNewsDAO newsDao;

    @Override
    public Result insertNews(EmBNews emBNews) throws DataAccessException {
        Result result = new Result();
        try {
            if (StringUtils.isBlank(emBNews.getNewsBrief())) {
                emBNews.setNewsBrief(HtmlHelper.Html2Text(emBNews.getNewsContent()));
            }
            if (emBNews.getNewsBrief().length() > 150) {
                emBNews.setNewsBrief(emBNews.getNewsBrief().substring(0, 150));
            }
            newsDao.insertEmBNews(emBNews);
            result.setResult("添加信息成功!");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseException(Result.CODE_100, "添加信息失败!");
        }
        return result;
    }

    @Override
    public Result searchPageEmBNews(EmBNews emBNews, Page<EmBNews> page) throws DataAccessException {
        Result result = new Result();
        try {
            result.setResultPage(newsDao.searchPageEmBNews(emBNews, page));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseException(Result.CODE_100, "查询异常");
        }
        return result;
    }

    @Override
    public Result searchMobilePageEmBNews(EmBNews emBNews, Page<EmBNews> page) throws DataAccessException {
        Result result = new Result();
        try {
            result.setResultPage(newsDao.searchMobilePageEmBNews(emBNews, page));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseException(Result.CODE_100, "查询异常");
        }
        return result;
    }

    @Override
    public EmBNews searchEmBNews(EmBNews emBNews) throws DataAccessException {
        EmBNews eb = newsDao.searchEmBNews(emBNews.getNewsId());
        try {
            emBNews.setViewAmount(1L);
            newsDao.updateEmBNews(emBNews);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return eb;
    }

    @Override
    public int deleteEmBNews(Long newsId) throws DataAccessException {
        int result = newsDao.deleteEmBNews(newsId);
        return result;
    }

    @Override
    public int deleteMultipleEmBNews(String newsIdList) {
        int rows = newsDao.deleteMultipleEmBNews(newsIdList);
        return rows;
    }

    @Override
    public Result updateEmBNews(EmBNews emBNews) throws DataAccessException {
        Result result = new Result();
        try {
            if (StringUtils.isBlank(emBNews.getNewsBrief())) {
                emBNews.setNewsBrief(HtmlHelper.Html2Text(emBNews.getNewsContent()));
            }
            if (emBNews.getNewsBrief().length() > 150) {
                emBNews.setNewsBrief(emBNews.getNewsBrief().substring(0, 150));
            }
            newsDao.updateEmBNews(emBNews);
            result.setResult("修改信息成功!");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseException(Result.CODE_100, "修改信息失败!");
        }
        return result;
    }

    @Override
    public Result updateEmBNewsBat(EmBNews emBNews) throws DataAccessException {
        Result result = new Result();
        try {
            newsDao.updateEmBNewsBat(emBNews);
            result.setResult("修改信息成功!");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseException(Result.CODE_100, "修改信息失败!");
        }
        return result;
    }

    @Cacheable(value = "news", key = "#emBNews.cacheKey", condition = "#emBNews.searchFlag eq 'tag'")
    @Override
    public Result searchSimpleNewsList(EmBNews emBNews) throws BaseException {
        Result ret = new Result();
        try {
            List<EmBNews> l = newsDao.searchSimpleNewsList(emBNews);
            ret.setRows(l);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            ret.setCodeResult(Result.CODE_100, "查询信息列表【SQL】异常");
        }
        return ret;
    }

}
